<?php
/**
 * 商户
 * User: chen guang
 * Date: 2017/9/29 9:00
 *
 */

namespace app\websys\Controller;


use think\Db;

class Set extends Common{
	
	/*
	*	构造函数
	*/
	function __construct(){
		parent::__construct();

	}

    /**
     * 首页分类
     */
    public function type(){
        $list = db("index_type")->order("id asc")->select();
        $this->assign('list',$list);
        return $this->fetch();
    }
	/**
    * 首页分类修改
    */
    public function type_edit(){
    	$id = input('id/d');
		if(empty($id)){
			$this->error("非法操作！");
		}
		$detail=array();
		if(request()->isPost()){
			$data['title']=input("title");
			$data['des']=trim(input('des'));
			$file=request()->file('images');
			if($file){
				$info = $file->move(ROOT_PATH . 'public' . DS . 'static' . DS . 'uploads');
				if($info){
					$data['images']= $info->getSaveName();
				}
			}
			$res=db("index_type")->where("id='$id'")->update($data);
			if($res===false) $this->error("操作失败！");
			$this->success("操作成功！");
		}
		$detail=db("index_type")->where("id='$id'")->find();
		$this->assign("data",$detail);
    	return $this->fetch();
    }
	/**
	 * banner图
	 */
	public function banner(){
		$banner = db('index_banner')->where("id=1") ->find();
		if(request()->isPost()){
			$file=request()->file('images');
			if($file){
				$info = $file->move(ROOT_PATH . 'public' . DS . 'static' . DS . 'uploads');
				if($info){
					$data['images']= $info->getSaveName();
				}
			}
			$files=request()->file('images_bj');
			if($files){
				$infos = $files->move(ROOT_PATH . 'public' . DS . 'static' . DS . 'uploads');
				if($infos){
					$data['images_bj']= $infos->getSaveName();
				}
			}
			$data['title'] = input('title');
			$data['des']   = input('des');
			$res = db('index_banner')->where("id=1")->update($data);
			if($res!==false){$this->success('操作成功');}else{$this->error('操作失败');}
		}
		$this -> assign("data",$banner);
		return $this->fetch();
	}

	/*
	 * 版块介绍
	 */
	public function introduce(){
		$list=db("index_introduce")->select();
		$this->assign("list",$list);
		return $this->fetch();
	}

	/*
      * 版块编辑
      */
	public function in_edit(){
		$id = input('id/d');
		if(empty($id)){
			$this->error('非法操作');
		}
		if(request()->isPost()){
			$file=request()->file('images');
			if($file){
				$info = $file->move(ROOT_PATH . 'public' . DS . 'static' . DS . 'uploads');
				if($info){
					$data['images']= $info->getSaveName();
				}
			}
			$data['title'] = input('title');
			$data['des']   = input('des');
			$res = db('index_introduce')->where("id='$id'")->update($data);
			if($res!==false){$this->success('操作成功');}else{$this->error('操作失败');}
		}
		$introduce = db('index_introduce')->where("id='$id'") ->find();
		$this -> assign("data",$introduce);
		return $this->fetch();
	}

	/*
      * 底部图编辑
      */
	public function foot_images(){
		if(request()->isPost()){
			$file=request()->file('images');
			if($file){
				$info = $file->move(ROOT_PATH . 'public' . DS . 'static' . DS . 'uploads');
				if($info){
					$data['images']= $info->getSaveName();
				}
			}
			$files=request()->file('images_bj');
			if($files){
				$infos = $files->move(ROOT_PATH . 'public' . DS . 'static' . DS . 'uploads');
				if($infos){
					$data['images_bj']= $infos->getSaveName();
				}
			}
			$res = db('index_banner')->where("id=2")->update($data);
			if($res!==false){$this->success('操作成功');}else{$this->error('操作失败');}
		}
		$images = db('index_banner')->where("id=2") ->find();
		$this -> assign("data",$images);
		return $this->fetch();
	}
	/**
	 * 关于我们
	 */
	public function about(){
		$about = db('about')->where("id=1") ->find();
		if(request()->isPost()){
			$file=request()->file('images');
			if($file){
				$info = $file->move(ROOT_PATH . 'public' . DS . 'static' . DS . 'uploads');
				if($info){
					$data['images']= $info->getSaveName();
				}
			}
			$data['title']    = input('title');
			$data['des']      = input('des');
			$data['tel']      = input('tel');
			$data['business'] = input('business');
			$data['product']  = input('product');
			$res = db('about')->where("id=1")->update($data);
			if($res!==false){$this->success('操作成功');}else{$this->error('操作失败');}
		}
		$this -> assign("data",$about);
		return $this->fetch();
	}
	/**
	 * 轮播图
	 */
	public function bannerlist(){
		$banner = db('banner');
		$ban = $banner->select();
		$this -> assign("res",$ban);
		return $this->fetch();
	}
	/**
	 * 轮播添加或修改
	 */
	public function banner_edit(){
		$id = input('id');
		if(request()->isPost()){
			$data['status'] = input('post.status');
			$data['title'] = input('post.title');
			$data['sort'] = input('post.displayorder');
			$data['url'] = input('post.url');
			$file = request()->file('bnimage');
			if (!empty($file)) {
				$info = $file->move(ROOT_PATH . 'public/static/uploads');
				if ($info) {
					$data['pic'] = $info->getSaveName();
				} else {
					$this->success($file->getError());
				}
			} else {
				$data['pic'] = input('bne');
			}
			if($id){
				$bnr = db('banner')->where("id='$id'")->update($data);
			}else{
				$bnr = db('banner')->insert($data);
			}
			$this->success("操作成功!",url('Set/bannerlist'));
		}
		$banner=db('banner')->where("id='$id'")->find();
		$this -> assign("banner",$banner);
		return $this->fetch();

	}
	/**
	 * 轮播删除
	 */
	public function banner_del(){
		$bnid = input('get.id');
		$banner = db('banner');
		$del = $banner -> where("id='$bnid'") -> delete();
		$this -> success("删除成功",url('Set/bannerlist'));
	}



}